var app = getApp();
const request = require('../../../../../utils/request')
const util = require('../../../../../utils/util');
let CodeUrlbg = request.BASE_URL + '/static/share/qrcode_head.png?new=1'; //背景图片
let shareTitle = request.BASE_URL + 'static/share/qrcode_head2.png?new=1'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //结算数量
    info:{},
    qrcode:'',
    template:'',
    navbarHeight:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    wx.getSystemInfo({
      success: function (res) {
          console.log(res, 34343)
          _this.setData({
              navbarHeight: util.getNavbarHeight(),
              statusBarHeight: res.statusBarHeight,
              // 屏幕宽高各乘2倍, 宽高保存到data, 供html用
              width: res.screenWidth,
              height: res.screenHeight,
              rpx: res.windowWidth / 750,

              screen_width: res.windowWidth,
              screen_height: res.windowHeight
          })

      }
    })
    this.getShareData()
  },
  getShareData(){
    var _this = this
    request._post('/api?method=user.tuiguangma', {
    }).then(res => {
      this.setData({
          recom_code: res.data.recom_code,
          qrcode: res.data.qrcode,
      })
      _this.getDraw()
    })
  },
  getDraw() {
    const {
      qrcode
    } = this.data
    var no_product = {
      background: '#fff',
      width: '750rpx',
      height: '1333rpx',
      views: [{
        type:'image',
        url: CodeUrlbg,
        css: {
          left: '0',
          top: '0',
          right: 0,
          width: '750rpx',
          height: '567rpx',
          display: 'block'
        },
      },{
        type:'image',
        url: shareTitle,
        css: {
          left: '81rpx',
          top: '192rpx',
          width: '322rpx',
          height: '105rpx',
          display: 'block'
        },
      },{
        type: 'image',
        url: '/pages/images/login-logo.png',
        css: {
          left: '311rpx',
          top: '358rpx',
          width: '128rpx',
          height: '201rpx',
          display: 'block',
        },
      },
      {
        type: 'rect',
        css: {
          left: '181rpx',
          top: '593rpx',
          width: '390rpx',
          height: '390rpx',
          display: 'block',
          color:'#E5E5E5',
          borderRadius: '30rpx'
        },
      },
      {
          type: 'image',
          url: qrcode,
          css: {
            left: '213rpx',
            top: '628rpx',
            width: '328rpx',
            height: '328rpx',
            display: 'block',
            borderRadius: '20rpx'
          },
        }
      ]
    }
    this.setData({
      template: no_product
    })
  },
  toBack() {
    wx.navigateBack()
  },
  canvasSuc(e) {
    wx.hideLoading()
    console.log(e)
    this.setData({
      image: e.detail.path
    })
  },
  downloadPoster: function () {
    let page = this,
    image = page.data.image
    if (image) {
        wx.saveImageToPhotosAlbum({
            filePath: image,
            success(res_) {
                wx.showToast({
                    title: '保存图片成功',
                })
            },
            fail(res_) {
                wx.showToast({
                    title: '保存图片失败',
                    icon: 'none'
                })
            }
        })
    }
  }
})